package com.ctgu.juc_project.source;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchTest {

    public static int threadCount = 3;
    public static int waitCount = 2;

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        ExecutorService runPool = Executors.newFixedThreadPool(threadCount);
        ExecutorService waitPool = Executors.newFixedThreadPool(waitCount);
        for (int i = 0; i < threadCount; i++) {
            runPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() + ": RUNNING");
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + ": WAITING");
        for (int j = 0; j < waitCount; j++) {
            waitPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() + ": WAITING");
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        runPool.shutdown();
        waitPool.shutdown();
    }

}
